home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc,owl.development
- Path: peer-news.britain.eu.net!uknet!owl-uk!news
- From: Kenn@owl-uk.co.uk (Ken Nicolson)
- Subject: Compiler Bug - can call private constructors.
- Message-ID: <311a149f.8353477@hector>
- Sender: news@owl-uk.co.uk (News system)
- Organization: Office Workstations Limited
- X-Newsreader: Forte Agent .99d/32.182
- Date: Thu, 8 Feb 1996 15:58:38 GMT
-
- Hi everyone,
-
- Running Visual C++ v4.0, I want declare the default constructor as being
- private, to ensure that the developer initialises the class with a string,
- say, or when I am implementing a Singleton, to ensure that noone else can
- create another instance of the class. However, I find that I can return (or
- throw) an object created by the default private constructor, without any
- warning from the compiler, from anywhere outside the class!
-
- Ken
-
- PS: Does anyone have an email address for Microsoft Tech Support? I've
- searched their website but cannot find anything. I don't fancy phoning
- their tech support line and spending half an hour describing a problem to
- someone who'll then forward it to someone else, who'll then forward it
- again...
-
- >>>> SAMPLE CODE REPRODUCING PROBLEM
-
- #include <stdio.h>
- #include <iostream.h>
-
- class FOOBAR
- {
- private:
- FOOBAR()
- {
- cout << "I've called a private constructor!\n";
- }
- };
-
-
- FOOBAR foobar()
- {
- // FOOBAR b; SYNTAX ERROR, as expected
- return FOOBAR( ); // This is wrong
- }
-